<template>
	<XnFormDrawerContainer
		:title="formData.id ? '编辑素材库' : '增加素材库'"
		:width="1300"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="素材类型：" name="materialType">
						<a-radio-group
							v-model:value="formData.materialType"
							placeholder="请选择素材类型"
							:options="materialTypeOptions"
							disabled
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="素材名称：" name="name">
						<a-input v-model:value="formData.name" placeholder="请输入素材名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="素材分类：" name="materialFullPath">
						<!--						<a-input v-model:value="formData.materialFullPath" placeholder="请输入素材分类" allow-clear />-->
						<a-cascader
							v-model:value="formData.parentPath"
							:options="cateTree"
							placeholder="请选择父级分类"
							expand-trigger="hover"
							change-on-select
							:fieldNames="{ label: 'name', value: 'id', children: 'children' }"
						/>
					</a-form-item>
				</a-col>

				<a-col :span="12">
					<a-form-item label="素材排序：" name="sortCode">
						<a-input v-model:value="formData.sortCode" placeholder="请输入素材排序" allow-clear />
					</a-form-item>
				</a-col>

				<a-col :span="12">
					<a-form-item label="素材封面(710*400)：" name="cover">
						<!--						<xn-upload v-model:value="formData.cover" uploadMode="image" />-->
						<edu-image-select-stock
							:image="formData.cover"
							:count="1"
							@update:image="(image) => (formData.cover = image)"
						></edu-image-select-stock>
					</a-form-item>
				</a-col>

				<template v-if="formData.materialType === 'video'">
					<a-col :span="12">
						<a-form-item label="素材链接：" name="materialUrl">
<!--							<a-button type="default">上传视频</a-button>-->
							<EduVodUpload v-model:value="formData.materialUrl" @successful="uploadSuccess"></EduVodUpload>
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item label="试看：" name="tryEnable">
							<a-switch v-model:checked="formData.tryEnable" />
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item label="试看时间：" name="tryDuration" v-if="formData.tryEnable">
							<a-input-number
								v-model:value="formData.tryDuration"
								:min="1"
								:step="1"
								placeholder="请输入试看时间"
								allow-clear
								style="width: 100%"
							/>
						</a-form-item>
					</a-col>
				</template>

				<template v-if="formData.materialType === 'audio'">
					<a-col :span="12">
						<a-form-item label="素材链接：" name="materialUrl">
							<a-button type="default">上传音频</a-button>
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item label="试听：" name="tryEnable">
							<a-switch v-model:checked="formData.tryEnable" />
						</a-form-item>
					</a-col>
					<a-col :span="12">
						<a-form-item label="试听时间：" name="tryDuration" v-if="formData.tryEnable">
							<a-input-number
								v-model:value="formData.tryDuration"
								:min="1"
								:step="1"
								placeholder="请输入试听时间"
								allow-clear
								style="width: 100%"
							/>
						</a-form-item>
					</a-col>
				</template>



				<template v-if="formData.materialType === 'img_txt'">

					<a-col :span="24">
						<a-form-item label="试看：" name="tryEnable">
							<a-switch v-model:checked="formData.tryEnable" />
						</a-form-item>
					</a-col>
					<a-col :span="24" v-if="formData.tryEnable">
						<a-form-item label="试看内容：" name="trySeeContent">
							<a-input v-model:value="formData.trySeeContent" placeholder="请输入素材内容" allow-clear />
						</a-form-item>
					</a-col>
					<a-col :span="24">
						<a-form-item label="素材内容：" name="materialContent">
 							<xn-editor v-model:value="formData.materialContent" placeholder="请输入素材内容" />
						</a-form-item>
					</a-col>
				</template>

				<a-col :span="24">
					<a-form-item label="素材简介：" name="materialBrief">
 						<xn-editor v-model:value="formData.materialBrief" placeholder="素材简介" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</XnFormDrawerContainer>
</template>

<script setup name="eduMaterialForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import eduMaterialApi from '@/api/biz/eduMaterialApi'
	import eduMaterialCateApi from '@/api/biz/eduMaterialCateApi'
	import EduImageSelectStock from '@/components/EduImgStock/edu-image-select-stock.vue'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const materialTypeOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		getCateTree()
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		materialTypeOptions.value = tool.dictList('MATERIAL_TYPE')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		name: [required('请输入素材名称')],
		cover: [required('请输入素材封面')],
		tryEnable: [required('请输入试看时间')],
		materialBrief: [required('请输入素材简介')],
		materialUrl: [required('请上传素材')],
		trySeeContent: [required('请输入试看内容')],
		materialContent: [required('请输入素材内容')],


	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				eduMaterialApi
					.eduMaterialSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}

	// 课程分类树
	const cateTree = ref([])
	const getCateTree = () => {
		eduMaterialCateApi.eduMaterialCateTree({ level: 2, includeTop: false }).then((tree) => {
			cateTree.value = tree
		})
	}

	const uploadSuccess = (info) => {
		console.log("uploadSuccessuploadSuccessuploadSuccess",info)
		formData.value.materialUrl = info.videoId
		formRef.value.validate('materialUrl')
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
